package com.draven.serviceImpl;

import com.draven.entity.News;
import com.draven.mapper.NewsMapper;
import com.draven.model.BaseConditionVO;
import com.draven.service.NewsService;
import com.draven.utils.SqlExcuteException;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Draven on 2017/4/19.
 * E-mail:draven1122@163.com
 */
@Service(NewsService.SERVICE_NAME)
@Transactional(rollbackFor = Exception.class)
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;

    public News getNews(String id) {
        News news = newsMapper.selectByPrimaryKey(id);
        return news;
    }

    public int addNews(News news) throws Exception {
        int i = 0;
        i = newsMapper.insert(news);
        if (i == 0){
            throw new SqlExcuteException("新增新闻失败");
        }
        return i;
    }

    public int addNewsSelective(News news) throws Exception {
        int i = 0;
        i = newsMapper.insert(news);
        if (i == 0){
            throw new SqlExcuteException("新增新闻失败");
        }
        return i;
    }

    public int removeNews(String id) throws Exception {
        int i = 0;
        i = newsMapper.deleteByPrimaryKey(id);
        if (i == 0){
            throw new SqlExcuteException("删除新闻失败");
        }
        return i;
    }

    public int updateNews(News news) throws Exception {
        int i = 0;
        i = newsMapper.updateByPrimaryKey(news);
        if (i == 0){
            throw new SqlExcuteException("更新新闻失败");
        }
        return i;
    }

    public int updateNewsSelective(News news) throws Exception {
        int i = 0;
        i = newsMapper.updateByPrimaryKeySelective(news);
        if (i == 0){
            throw new SqlExcuteException("更新新闻失败");
        }
        return i;
    }

    public List<News> listNews(BaseConditionVO vo) throws Exception {
        List<News> news = new ArrayList<News>();
        RowBounds rb = new RowBounds(vo.getStartIndex(),vo.getRows());
        news = newsMapper.listNews(vo,rb);
        return news;
    }

    public int countNews(BaseConditionVO vo) throws Exception {
        int i = 0;
        i = newsMapper.countNews(vo);
        return i;
    }
}
